package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.ProductTypeDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.param.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.product.form.UpdateProductTypeForm;
import com.woniu108.product.service.ProductTypeService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品种类表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productType")
@Api(tags = "商品种类接口")
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 查询所有商品种类
     * @return 商品种类详情集合
     */
    @GetMapping("/findAllProductType.api")
    @ApiOperation("查询所有商品种类")
    public Result<List<ProductTypeDto>> findAllProductType(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        List<ProductTypeDto> dtos = productTypeService.findAllProductType(adminId);
        return Result.newListInstance(ProductTypeDto.class).setData(dtos).setMsg("查询所有商品种类成功");
    }
    /**
     *查询所有正常商品种类
     * @return 商品种类详情集合
     */
    @GetMapping("/findAllNormalProductType.c")
    @ApiOperation("查询所有正常商品种类")
    public Result<List<ProductTypeDto>> findAllNormalProductType() {
         List<ProductTypeDto> dtos = productTypeService.findAllNormalProductType();
        return Result.newListInstance(ProductTypeDto.class).setData(dtos).setMsg("查询所有正常商品种类成功");
    }
    /**
     * 新增商品种类
     * @param  pTName 种类名
     * @return boolean值
     */
    @GetMapping("/insertProductType.api")
    @ApiOperation("新增商品种类")@AdminHistoryLog
    @ApiImplicitParam(name = "pTName", value = "种类名")
    public Result<Boolean> insertProductType(String pTName, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertProductTypeParam param = new InsertProductTypeParam(pTName, adminId);
        Boolean bool = productTypeService.insertProductType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增商品种类成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增商品种类失败").setData(false).setCode(500);
    }
    /**
     * 修改商品种类
     * @param pTId 种类id
     * @param pTName 新种类名
     * @return boolean值
     */
    @GetMapping("/updateProductType.api")
    @ApiOperation("修改商品种类名")@AdminHistoryLog
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pTId",value ="种类id" ),
            @ApiImplicitParam(name = "pTName",value ="新种类名" )
    })
    public Result<Boolean> updateProductType(Integer pTId,String pTName , @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateProductTypeParam param =new UpdateProductTypeParam();
        param.setPTName(pTName);
        param.setPTId(pTId);
        param.setAdminId(adminId);
        Boolean bool = productTypeService.updateProductType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改商品种类名成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改商品种类名失败").setData(false).setCode(500);
    }
    /**
     * 禁用商品种类
     * @param pTIds 种类id集合
     * @return boolean值
     */
    @PostMapping("/banProductType.api")
    @ApiOperation("禁用商品种类")@AdminHistoryLog
    @ApiImplicitParam(name = "pTIds", value = "种类id集合")
    public Result<Boolean> banProductType(@RequestBody List<Integer> pTIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanProductTypeParam param = new BanProductTypeParam(pTIds, adminId);
        Boolean bool = productTypeService.banProductType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用商品种类成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用商品种类失败").setData(false).setCode(500);
    }
    /**
     * 解锁商品种类
     * @param pTIds 种类id集合
     * @return boolean值
     */
    @PostMapping("/unlockProductType.api")
    @ApiOperation("解锁商品种类")@AdminHistoryLog
    @ApiImplicitParam(name = "pTIds", value = "种类id集合")
    public Result<Boolean> unlockProductType(@RequestBody List<Integer> pTIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockProductTypeParam param = new UnlockProductTypeParam(pTIds, adminId);
        Boolean bool = productTypeService.unlockProductType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁商品种类成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁商品种类失败").setData(false).setCode(500);
    }
    /**
     * 删除商品种类
     * @param pTIds 种类id集合
     * @return boolean值
     */
    @PostMapping("/deleteProductType.api")
    @ApiOperation("删除商品种类")@AdminHistoryLog
    @ApiImplicitParam(name = "pTIds", value = "种类id集合")
    public Result<Boolean> deleteProductType(@RequestBody List<Integer> pTIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteProductTypeParam param = new DeleteProductTypeParam(pTIds, adminId);
        Boolean bool = productTypeService.deleteProductType(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除商品种类成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除商品种类失败").setData(false).setCode(500);
    }
}

